package com.dou.yygh.cmn.controller;

import com.dou.yygh.cmn.service.DictService;
import com.dou.yygh.common.result.Result;
import com.dou.yygh.model.cmn.Dict;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created with IntelliJ IDEA 2021.1 x64
 *
 * @Author: 窦义超
 * @Date: 2021/08/2021/8/24/14:23
 * @Description:
 */
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    DictService dictService;

    //导入数据字典
    @PostMapping("importData")
        public Result importDict(MultipartFile file){
            dictService.importDictData(file);
            return Result.ok();
    }
    //导出数据字典
    @GetMapping("exportData")
    public void exportDict(HttpServletResponse response){
        dictService.exportDictData(response);
    }
//  根据dictCode获取下级节点
@ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("findByDictCode/{dictCode}")
public Result findDictCode(@PathVariable String dictCode){
   List<Dict> list= dictService.findByDictCode(dictCode);
   return Result.ok(list);
}


    //根据数据id查询子数据列表
    @ApiOperation(value = "根据id查询子数据列表")
    @GetMapping("findChildDate/{id}")
    public Result findChildData(@PathVariable long id){
      List<Dict> list=   dictService.findChildData(id);
      return Result.ok(list);
    }
    //根据dictcode和value查询
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value) {
        String dictName = dictService.getDictName(dictCode,value);
        return dictName;
    }

    //根据value查询
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value) {
        String dictName = dictService.getDictName("",value);
        return dictName;
    }
}
